package com.app.service.impl;

import com.app.enums.StatusCodeEnum;
import com.app.mapper.BrowseNumberMapper;
import com.app.mapper.UserMapper;
import com.app.model.BrowseNumberModel;
import com.app.model.JobPublishModel;
import com.app.model.UserModel;
import com.app.model.WorkApplyModel;
import com.app.service.BrowseNumberService;
import com.app.service.MsgVerifyService;
import com.app.util.CommonHelp;
import com.app.util.StringTool;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @Description:访问量实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
@Log4j2
public class BrowseNumberServiceImpl implements BrowseNumberService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BrowseNumberMapper browseNumberMapper;

    @Override
    public int addBrowse(String token) {

        if (StringTool.isNotEmpty(token)) {
            String mobile = TokenUtils.nothingness(token);
            if (mobile == null) {
                return StatusCodeEnum.STATUS405.getCode();
            }
            QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.select().eq("mobile", mobile);
            List<UserModel> userList = userMapper.selectList(queryWrapper);
            if (userList.isEmpty()) {
                return StatusCodeEnum.STATUS406.getCode();
            }
            UserModel user = userList.get(0);
            BrowseNumberModel model = new BrowseNumberModel();
            model.setUserId(user.getId());
            model.setCrtTime(CommonHelp.getNowTimestamp());
            browseNumberMapper.insert(model);
        } else {
            BrowseNumberModel model = new BrowseNumberModel();
            model.setCrtTime(CommonHelp.getNowTimestamp());
            browseNumberMapper.insert(model);
        }

        return 200;
    }

    @Override
    public int addBrowse(String token, Integer type) {

        if (StringTool.isNotEmpty(token)) {
            String mobile = TokenUtils.nothingness(token);
            if (mobile == null) {
                return StatusCodeEnum.STATUS405.getCode();
            }
            QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.select().eq("mobile", mobile);
            List<UserModel> userList = userMapper.selectList(queryWrapper);
            if (userList.isEmpty()) {
                return StatusCodeEnum.STATUS406.getCode();
            }
            UserModel user = userList.get(0);
            BrowseNumberModel model = new BrowseNumberModel();
            model.setUserId(user.getId());
            model.setType(type);
            model.setCrtTime(CommonHelp.getNowTimestamp());
            browseNumberMapper.insert(model);
        } else {
            BrowseNumberModel model = new BrowseNumberModel();
            model.setType(type);
            model.setCrtTime(CommonHelp.getNowTimestamp());
            browseNumberMapper.insert(model);
        }

        return 200;
    }

    @Override
    public int getBrowseCount(String startTime,String endTime,Integer type) {
        QueryWrapper<BrowseNumberModel> queryWrapper = new QueryWrapper<>();
        if(type==1)
        {
            queryWrapper.apply(" TO_DAYS(crt_time) >= TO_DAYS('"+startTime+"')");
            queryWrapper.apply(" TO_DAYS(crt_time) <= TO_DAYS('"+endTime+"')");
        }else{
            queryWrapper.apply(" TO_DAYS(crt_time) > TO_DAYS('2023-07-14 00:00:00')");
        }
        queryWrapper.select().orderByDesc("id");
        return browseNumberMapper.selectCount(queryWrapper);
    }

    @Override
    public int getBrowseCount(String todayStart, String todayEnd) {
        QueryWrapper<BrowseNumberModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("crt_time", todayStart, todayEnd);
        return browseNumberMapper.selectCount(queryWrapper);
    }

    @Override
    public int getModelCountByTime(String crtTime) {
        QueryWrapper<BrowseNumberModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("TO_DAYS(crt_time) >= TO_DAYS('" + crtTime + "')");
        return browseNumberMapper.selectCount(queryWrapper);
    }

    @Override
    public List<BrowseNumberModel> selectAll(Integer page, Integer limit) {
        return browseNumberMapper.selectAll(page,limit);
    }

    @Override
    public int getCount(Integer type) {
        QueryWrapper<BrowseNumberModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        return browseNumberMapper.selectCount(queryWrapper);
    }
}
